package cn.com.jsepc.rep.report;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import net.sf.json.JSONArray;
import cn.com.jsepc.rep.comm.ConstUtil;
import cn.com.jsepc.rep.vo.FieldVo;
import cn.com.jsepc.rep.vo.FieldDataVO;

import com.jarp.db.DAO;
import javax.servlet.ServletException;
import net.sf.json.JSONObject;

public class DatatbGrid {
    
   //加载列
    public String getGrid()
    {
        String res = "";     
        DAO dao = new DAO();
        try
        {
            List<FieldVo> lstResults = new ArrayList<FieldVo>();
            String sql = "select * from TF_REP_HRCNT";   
            
            Vector v = dao.getDataSet(ConstUtil.DBNAME, sql);
            if (v != null)
            {
            	
                for (int i = 0; i < v.size(); i++)
                {
                    Hashtable hashRows = (Hashtable)v.get(i);
                    
                    FieldVo fieldVO = new FieldVo();
                    fieldVO.setHeader((String)hashRows.get("DEMNAME"));
                    fieldVO.setDataIndex((String)hashRows.get("IDCOL"));
                    lstResults.add(fieldVO);
            
                    if (i==v.size()-1){
                    	FieldVo fieldVO1 = new FieldVo();
	                    fieldVO1.setHeader((String)hashRows.get("REPNAME"));
	                    fieldVO1.setDataIndex((String)hashRows.get("REPVALUECOL"));
	                    lstResults.add(fieldVO1);
                    }
           
                }         
                JSONArray json = new JSONArray();
                //int totalCount = 100;
                json = JSONArray.fromObject(lstResults);
                //res = "{totalCount:"+totalCount+",data:"+json.toString()+"}";
                res=json.toString();
                
            }   
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            System.out.println("NewProjectTree getXmDl Exception.");
        }
        return res;
    }
//    
//    
//    
//	
//	//加载列
//	public String getGrid()
//       {
//        String res = "";     
//        DAO dao = new DAO();
//        try
//        {
//        	List<FieldVo> lstResults = new ArrayList<FieldVo>();
//                //List<FieldVo> lstResults1 = new ArrayList<FieldVo>();
//            String sql = "select * from ( select * from tb_report where repid=12 ) a" +
//    		" left join tb_rep_demension b on b.repid=a.repid" +
//    		" left join tb_demension c on c.demid=b.dimid ";         
//            
//            Vector v = dao.getDataSet(ConstUtil.DBNAME, sql);
//            
//            if (v != null)
//            {
//            	
//                for (int i = 0; i < v.size(); i++)
//                {
//                    Hashtable hashRows = (Hashtable)v.get(i);
//                    
//                    FieldVo fieldVO = new FieldVo();
//                    fieldVO.setHeader((String)hashRows.get("DEMNAME"));
//                    fieldVO.setDataIndex((String)hashRows.get("IDCOL"));
//                    lstResults.add(fieldVO);  
//                    
////                    FieldVo fieldVO2 = new FieldVo();
////                    fieldVO2.setName((String)hashRows.get("REPVALUECOL")); 
////                    lstResults1.add(fieldVO2);
//                    
//                    if (i==v.size()-1){
//                    	FieldVo fieldVO1 = new FieldVo();
//	                    fieldVO1.setHeader((String)hashRows.get("REPNAME"));
//	                    fieldVO1.setDataIndex((String)hashRows.get("REPVALUECOL"));
//	                    lstResults.add(fieldVO1);
//                           
//                    }
//           
//                }         
//                JSONArray json = new JSONArray();
//               // JSONArray json1 = new JSONArray();
//                json = JSONArray.fromObject(lstResults);
//               // json1 = JSONArray.fromObject(lstResults1);
//               // res = "{columnModel:"+json.toString()+"fieldsNames:"+json1.toString()+"}";
//                res="{columnModel:"+json.toString()+"}";
//                System.out.println(res);
//            }   
//        }
//        catch (Exception ex)
//        {
//            ex.printStackTrace();
//            System.out.println("NewProjectTree getXmDl Exception.");
//        }
//        return res;
//    }
//	

	

	//加载列数据
	public String getDataGrid(String start,String limit)
         {
		String res = "";        
	        DAO dao = new DAO();
	        try
	        {
	            ArrayList<FieldDataVO> lstResults = new ArrayList<FieldDataVO>();
                    int iStart = Integer.parseInt(start)+1;
	            int iEnd = Integer.parseInt(start)+Integer.parseInt(limit);
                    
	            String sql = "select * from (select a.*,rownum rn from (select * from TF_REP_HRCNT)a )where rn between "+iStart+" and "+ iEnd;  
	           // System.out.println(sql);
	            Vector v = dao.getDataSet(ConstUtil.DBNAME, sql);
	            if (v != null)
	            {
	                for (int i = 0; i < v.size(); i++)
	                {
	                    Hashtable hashRows = (Hashtable)v.get(i); 
	                    
	                    FieldDataVO fieldDataVO = new FieldDataVO();
	                    fieldDataVO.setOrg_id(Integer.parseInt((String)hashRows.get("ORG_ID")));
	                    fieldDataVO.setTime_id(Integer.parseInt((String)hashRows.get("TIME_ID")));
	                    fieldDataVO.setHtcnt(Integer.parseInt((String)hashRows.get("HTCNT")));
           
	                    lstResults.add(fieldDataVO);
	                }
	
		
                JSONArray json = new JSONArray();
                String sql1="select count(*) CNT from TF_REP_HRCNT";        
                String totalCount=dao.getDataValue(ConstUtil.DBNAME, sql1);
                json = JSONArray.fromObject(lstResults);
                res = "{totalCount:"+totalCount+",data:"+json.toString()+"}";
            }   
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            System.out.println("NewProjectTree getXmDl Exception.");
        }
        return res;
    }	
}

//   	//加载列
//	public String getGrid()
//    {
//        String res = "";     
//        DAO dao = new DAO();
//        try
//        {
//        	List<FieldVo> lstResults = new ArrayList<FieldVo>();
//            String sql = "select * from ( select * from tb_report where repid=12 ) a" +
//    		" left join tb_rep_demension b on b.repid=a.repid" +
//    		" left join tb_demension c on c.demid=b.dimid ";   
//            System.out.println(sql);
//            
//            Vector v = dao.getDataSet(ConstUtil.DBNAME, sql);
//            System.out.println(v);
//            if (v != null)
//            {
//            	
//                for (int i = 0; i < v.size(); i++)
//                {
//                    Hashtable hashRows = (Hashtable)v.get(i);
//                    
//                    FieldVo fieldVO = new FieldVo();
//                    fieldVO.setHeader((String)hashRows.get("DEMNAME"));
//                    fieldVO.setDataIndex((String)hashRows.get("IDCOL"));
//                    lstResults.add(fieldVO);
//            
//                    if (i==v.size()-1){
//                    	FieldVo fieldVO1 = new FieldVo();
//	                    fieldVO1.setHeader((String)hashRows.get("REPNAME"));
//	                    fieldVO1.setDataIndex((String)hashRows.get("REPVALUECOL"));
//	                    lstResults.add(fieldVO1);
//                    }
//           
//                }         
//                JSONArray json = new JSONArray();
//                //int totalCount = 100;
//                json = JSONArray.fromObject(lstResults);
//                //res = "{totalCount:"+totalCount+",data:"+json.toString()+"}";
//                res=json.toString();
//                
//                System.out.println("columns:"+res);
//            }   
//        }
//        catch (Exception ex)
//        {
//            ex.printStackTrace();
//            System.out.println("NewProjectTree getXmDl Exception.");
//        }
//        return res;
//    }










